/* * * * M A P V I S I O N   L I C E N S E  *****************************************************************
 *
 * This MapVision software is © Filip Zavadil 2013. It is licensed under the LGPL license.
 * This MapVision software dynamically links to unmodified Qt5 Library.
 *
 * The Qt5 Library is © 2013 Qt® and/or its subsidiary(-ies), and is licensed under the
 * GNU Lesser General Public License version 2.1 with Qt® LGPL exception version 1.1.
 * Qt5 library is free software; you can redistribute it and/or modify it under the terms of the
 * GNU Lesser General Public License, version 2.1, as published by the Free Software Foundation.
 * Qt5 library is provided “AS IS”, without WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
 *
 * As an additional permission to the GNU Lesser General Public License version 2.1, the object
 * code form of a “work that uses the Library” may incorporate material from a header file that is
 * part of the Library. You may distribute such object code under terms of your choice,
 * provided that: (i) the header files of the Library have not been modified; and (ii)
 * the incorporated material is limited to numerical parameters, data structure layouts, accessors,
 * macros, inline functions and templates; and (iii) you comply with the terms of Section 6 of the
 * GNU Lesser General Public License version 2.1.
 *
 * Moreover, you may apply this exception to a modified version of the Library, provided that such
 * modification does not involve copying material from the Library into the modified Library’s header
 * files unless such material is limited to (i) numerical parameters; (ii) data structure layouts;
 * (iii) accessors; and (iv) small macros, templates and inline functions of five lines or less in length.
 *
 * Furthermore, you are not required to apply this additional permission to a modified version of the Library.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this package;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

 * The source code for Qt 5.X SDK is available from Qt Project here:
 * http://download.qt-project.org/official_releases/qtcreator/2.7/2.7.0/qt-creator-2.7.0-src.zip.
 *
 * ****************************** M A P V I S I O N    S O F T W A R E  ************************************
 *
 * Author:       Filip Zavadil
 * Description:  Software for displaying and exporting map tiles.
 * The sources and executables of MapVision software are available:
 *    - from project hosting on CodePlex.com :   http://mapvision.codeplex.com
 *    - from GIT repositories on Gitorious.org:  https://gitorious.org/mapvision
 * Visit MapVision fanpage on Facebook.com:      https://www.facebook.com/pages/Mapvision/631827753513935
 *
 * *********************************************************************************************************
*/

#include "dockwidgettext.h"
#include "ui_dockwidgettext.h"
#include "graphicstextitem.h"
#include <QFont>
#include "mapvision.h"

DockWidgetText::DockWidgetText(MapVision *parent) :
    DockWidgetGeneral(parent),
    ui(new Ui::DockWidgetText)
{
    ui->setupUi(this);
    this->setWindowTitle(tr("Text"));

    connect(getMapView(),SIGNAL(mousePressed(QMouseEvent*)),this,SLOT(addText(QMouseEvent*)));

    items = new QList<QGraphicsTextItem*>;

    colorDialog.setCurrentColor(Qt::black);


}

void DockWidgetText::feedValues(QGraphicsTextItem * item)
{
    QFont font = item->font();
    ui->fontComboBox->setCurrentFont(font);
    ui->spinBoxTextSize->setValue(font.pixelSize());
    ui->toolButtonBold->setChecked(font.bold());
    ui->toolButtonItalic->setChecked(font.italic());
    ui->toolButtonOverLine->setChecked(font.overline());
    ui->toolButtonUnderline->setChecked(font.underline());
    ui->toolButtonColor->setStyleSheet("background-color: "+item->defaultTextColor().name()+";");
    colorDialog.setCurrentColor(item->defaultTextColor());
}

DockWidgetText::~DockWidgetText()
{
    delete ui;
}

void DockWidgetText::addText(QMouseEvent* event)
{
    if (!ui->toolButtonAddText->isChecked()) return;

    GraphicsTextItem * item = new GraphicsTextItem(this,ui->lineEditText->text());
    getMapView()->getScene()->addItem(item);

    QFont font(ui->fontComboBox->currentFont());
    font.setPixelSize(ui->spinBoxTextSize->value());
    font.setBold(ui->toolButtonBold->isChecked());
    font.setItalic(ui->toolButtonItalic->isChecked());
    font.setUnderline(ui->toolButtonUnderline->isChecked());
    font.setOverline(ui->toolButtonOverLine->isChecked());
    item->setFont(font);


    ui->toolButtonAddText->setChecked(false);
    getMapView()->getScene()->clearSelection();
    item->setSelected(true);
    item->setPos(getMapView()->mapToScene(event->pos())-item->boundingRect().center());
    item->setDefaultTextColor(colorDialog.currentColor());
    items->push_back(item);

    getActionsGroup()->actions()[2]->toggle();
    item->setSelected(true);
}

void DockWidgetText::updateLineEditText(QString updatedText)
{
    ui->lineEditText->setText(updatedText);
}

void DockWidgetText::on_toolButtonAddText_toggled(bool checked)
{ if (checked) getMapView()->viewport()->setCursor(Qt::IBeamCursor);
    else getMapView()->viewport()->setCursor(getMapView()->lastCursor);
}

void DockWidgetText::on_fontComboBox_currentFontChanged(const QFont &f)
{
    if (!ui->fontComboBox->hasFocus()) return;
    Q_UNUSED(f)
    updateFont();

}

void DockWidgetText::updateFont()
{
    for (int i =0; i<items->size();i++){
        QGraphicsTextItem * item = items->value(i);
        if (item->isSelected()){
            QFont font(ui->fontComboBox->currentFont());
            font.setPixelSize(ui->spinBoxTextSize->value());
            font.setBold(ui->toolButtonBold->isChecked());
            font.setItalic(ui->toolButtonItalic->isChecked());
            font.setUnderline(ui->toolButtonUnderline->isChecked());
            font.setOverline(ui->toolButtonOverLine->isChecked());
            item->setFont(font);
            item->setDefaultTextColor(colorDialog.currentColor());
        }
    }
}

void DockWidgetText::on_spinBoxTextSize_valueChanged(int arg1)
{
    if (!ui->spinBoxTextSize->hasFocus()) return;
    Q_UNUSED(arg1)
    updateFont();
}

void DockWidgetText::on_toolButtonBold_clicked()
{
    updateFont();
}

void DockWidgetText::on_toolButtonItalic_clicked()
{
    updateFont();
}

void DockWidgetText::on_toolButtonUnderline_clicked()
{
    updateFont();
}

void DockWidgetText::on_toolButtonOverLine_clicked()
{
    updateFont();
}


void DockWidgetText::on_toolButtonColor_clicked()
{
    QColor color = colorDialog.getColor(Qt::black, this, "Text Color",  QColorDialog::DontUseNativeDialog);
    colorDialog.setCurrentColor(color);
    ui->toolButtonColor->setStyleSheet("background-color: "+color.name()+";");
    updateFont();
}


void DockWidgetText::on_lineEditText_textChanged(const QString &arg1)
{
    for (int i =0; i<items->size();i++){
        QGraphicsTextItem * item = items->value(i);
        if (item->isSelected()){
            item->setPlainText(arg1);
        }
    }
}
